package org.example;

import org.example.model.Command.Command;
import org.example.model.Command.StartTaskCommand;
import org.example.model.Command.StopTaskCommand;
import org.example.model.Factory.FarmMachineFactory;
import org.example.model.Factory.HarvesterFactory;
import org.example.model.Factory.SeederFactory;
import org.example.model.Factory.SprayerFactory;
import org.example.model.MachineState.IdleState;
import org.example.model.MachineState.MaintenanceState;
import org.example.model.MachineState.WorkingState;
import org.example.model.Observable.EnvironmentSensor;
import org.example.model.PathStrategy.AvoidObstacleStrategy;
import org.example.model.PathStrategy.NormalPathStrategy;
import org.example.model.PathStrategy.OptimizedPathStrategy;
import org.example.model.SafetyHandler.BoundaryHandler;
import org.example.model.SafetyHandler.FaultHandler;
import org.example.model.SafetyHandler.ObstacleHandler;
import org.example.model.SafetyHandler.SafetyHandler;
import org.example.model.Singleton.NavigationSystem;
import org.example.other.EventType;
import org.example.other.FieldData;
import org.example.other.SafetyEvent;
import org.example.other.SensorData;
import org.example.system.FarmMachine;
import org.example.system.Harvester;
import org.example.system.Seeder;

import java.util.Scanner;

public class SmartFarmSystemOne
{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("=============== 智能无人驾驶农机系统 ===============");
        System.out.println("请选择要演示的设计模式（输入对应编号）：");
        System.out.println("1. 工厂方法模式 - 农机设备创建");
        System.out.println("2. 观察者模式 - 环境监测与响应");
        System.out.println("3. 策略模式 - 动态路径规划");
        System.out.println("4. 责任链模式 - 安全事件处理");
        System.out.println("5. 状态模式 - 农机状态管理");
        System.out.println("6. 命令模式 - 操作指令封装");
        System.out.println("7. 单例模式 - 定位导航系统");
        System.out.println("0. 退出系统");

        while (true) {
            System.out.print("\n请输入选择（0-7）：");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    demoFactoryPattern();
                    break;
                case 2:
                    demoObserverPattern();
                    break;
                case 3:
                    demoStrategyPattern();
                    break;
                case 4:
                    demoChainOfResponsibility();
                    break;
                case 5:
                    demoStatePattern();
                    break;
                case 6:
                    demoCommandPattern();
                    break;
                case 7:
                    demoSingletonPattern();
                    break;

                case 0:
                    System.out.println("系统已退出");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效输入，请重新选择");
            }
        }
    }

    // 1. 工厂方法模式演示
    private static void demoFactoryPattern() {
        System.out.println("\n=== 工厂方法模式演示 ===");
        System.out.println("1. 创建收割机");
        System.out.println("2. 创建播种机");
        System.out.println("3. 创建喷药机");
        System.out.print("请选择农机类型：");

        Scanner scanner = new Scanner(System.in);
        int type = scanner.nextInt();

        FarmMachineFactory factory = null;
        switch (type) {
            case 1:
                factory = new HarvesterFactory();
                break;
            case 2:
                factory = new SeederFactory();
                break;
            case 3:
                factory = new SprayerFactory();
                break;
            default:
                System.out.println("无效选择");
                return;
        }

        FarmMachine machine = factory.createMachine("M-" + System.currentTimeMillis() % 1000);
        System.out.println("创建成功！");
        machine.displayStatus();
    }

    // 2. 观察者模式演示
    private static void demoObserverPattern() {
        System.out.println("\n=== 观察者模式演示 ===");
        FarmMachine harvester = new Harvester("H-001");
        FarmMachine seeder = new Seeder("S-001");

        EnvironmentSensor moistureSensor = new EnvironmentSensor("土壤湿度");
        moistureSensor.addObserver(harvester);
        moistureSensor.addObserver(seeder);

        System.out.println("模拟传感器数据变化：");
        moistureSensor.setData(new SensorData("土壤湿度", 85.0));

        System.out.println("\n移除播种机观察者后再次测试：");
        moistureSensor.removeObserver(seeder);
        moistureSensor.setData(new SensorData("土壤湿度", 75.0));
    }

    // 3. 策略模式演示
    private static void demoStrategyPattern() {
        System.out.println("\n=== 策略模式演示 ===");
        FarmMachine harvester = new Harvester("H-001");

        System.out.println("1. 标准路径规划");
        System.out.println("2. 避障路径规划");
        System.out.println("3. 协同优化路径");
        System.out.print("请选择路径策略：");

        Scanner scanner = new Scanner(System.in);
        int strategy = scanner.nextInt();

        switch (strategy) {
            case 1:
                harvester.setPathStrategy(new NormalPathStrategy());
                break;
            case 2:
                harvester.setPathStrategy(new AvoidObstacleStrategy());
                break;
            case 3:
                harvester.setPathStrategy(new OptimizedPathStrategy());
                break;
            default:
                System.out.println("无效选择");
                return;
        }

        FieldData fieldData = new FieldData("A-1");
        fieldData.addReading("障碍物", 1);
        harvester.planPath(fieldData);
    }

    // 4. 责任链模式演示
    private static void demoChainOfResponsibility() {
        System.out.println("\n=== 责任链模式演示 ===");
        SafetyHandler obstacleHandler = new ObstacleHandler();
        SafetyHandler faultHandler = new FaultHandler();
        SafetyHandler boundaryHandler = new BoundaryHandler();

        obstacleHandler.setNext(faultHandler);
        faultHandler.setNext(boundaryHandler);

        System.out.println("1. 障碍物事件");
        System.out.println("2. 设备故障事件");
        System.out.println("3. 边界告警事件");
        System.out.print("请选择安全事件类型：");

        Scanner scanner = new Scanner(System.in);
        int eventType = scanner.nextInt();

        EventType type = null;
        switch (eventType) {
            case 1:
                type = EventType.OBSTACLE;
                break;
            case 2:
                type = EventType.MACHINE_FAULT;
                break;
            case 3:
                type = EventType.BOUNDARY_ALERT;
                break;
            default:
                System.out.println("无效选择");
                return;
        }

        obstacleHandler.handle(new SafetyEvent(type));
    }

    // 5. 状态模式演示
    private static void demoStatePattern() {
        System.out.println("\n=== 状态模式演示 ===");
        FarmMachine harvester = new Harvester("H-001");

        System.out.println("1. 空闲状态");
        System.out.println("2. 工作状态");
        System.out.println("3. 维护状态");
        System.out.print("请选择状态：");

        Scanner scanner = new Scanner(System.in);
        int state = scanner.nextInt();

        switch (state) {
            case 1:
                harvester.setState(new IdleState());
                break;
            case 2:
                harvester.setState(new WorkingState());
                break;
            case 3:
                harvester.setState(new MaintenanceState());
                break;
            default:
                System.out.println("无效选择");
                return;
        }

        harvester.displayStatus();
    }

    // 6. 命令模式演示
    private static void demoCommandPattern() {
        System.out.println("\n=== 命令模式演示 ===");
        FarmMachine harvester = new Harvester("H-001");

        System.out.println("1. 开始收割任务");
        System.out.println("2. 停止当前任务");
        System.out.print("请选择命令：");

        Scanner scanner = new Scanner(System.in);
        int cmd = scanner.nextInt();

        Command command = null;
        switch (cmd) {
            case 1:
                command = new StartTaskCommand(harvester, "北区小麦收割");
                break;
            case 2:
                command = new StopTaskCommand(harvester);
                break;
            default:
                System.out.println("无效选择");
                return;
        }

        command.execute();
    }
//
//    // 7. 适配器模式演示
//    private static void demoAdapterPattern() {
//        System.out.println("\n=== 适配器模式演示 ===");
//        NewWeeder newWeeder = new NewWeeder();
//        FarmMachine weederAdapter = new NewWeederAdapter("W-001", newWeeder);
//
//        System.out.println("通过适配器调用新设备：");
//        weederAdapter.startTask("东区除草作业");
//        weederAdapter.displayStatus();
//    }
//
    // 8. 单例模式演示
    private static void demoSingletonPattern() {
        System.out.println("\n=== 单例模式演示 ===");
        NavigationSystem nav1 = NavigationSystem.getInstance();
        NavigationSystem nav2 = NavigationSystem.getInstance();

        System.out.println("导航系统实例1位置：" + nav1.getPosition());
        System.out.println("导航系统实例2位置：" + nav2.getPosition());
        System.out.println("是否为同一实例：" + (nav1 == nav2));
    }
}